package rad.framework.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class EnumConverter implements Converter {

	public Object getAsObject(FacesContext context, UIComponent comp,
			String value) throws ConverterException {
//		System.out.println("getAsObject: " + value);
		if (value == null || value.trim().length() == 0) {
			return null;
		}
		Class enumType = comp.getValueBinding("value").getType(context);
		return Enum.valueOf(enumType, value);
	}

	public String getAsString(FacesContext ctx, UIComponent comp, Object object)
			throws ConverterException {
//		System.out.println("getAsString: " + object);
		if (object == null) {
			return null;
		}
		if (object instanceof String) {
			return object.toString();
		}
		Enum type = (Enum) object;
		return type.toString();
	}

}
